<?php

include_once("../../../common/module/checkA.php");
include_once("../../../../config.php");
include_once ("../../../coresection/cls_json.php");

session_start();

$admin_user = $_SESSION['username'] !='' ? $_SESSION['username'] : $_SESSION['mid'];



$result = array('error' => 1, 'message' => '', 'content' => '');
$json  = new JSON;

if($admin_user  == ''){

	$result['error'] = "1";
	$result['content'] = "未知错误，请重新登陆操作";

	die($json->encode($result));
	exit;

}

//设置需要搜索的表格的前缀后缀
$table_suffix='order_list';//$plate['table_suffix'];
$table=$table_Prefix.$table_suffix;

$new_detail_nid=$_REQUEST['nid'];

$act = $_REQUEST['act'];

if($act == 'all_in'){

	$id_arr = explode(',',$new_detail_nid);

	foreach($id_arr as $val){

		$order_info = select_one_line_all_items($table_Prefix.$table_suffix ,"order_id = ".$val);

		if($order_info['order_status'] == 0){
			$res = do_query(" update $table set order_status = '2' WHERE order_id='$val'");

			//取消订单增加对应的库存
			$updateStorageSql =  "UPDATE ".$table_Prefix."dishes d,".$table_Prefix."order_detail o SET d.storage = d.storage + o.goods_number WHERE d.product_id = o.goods_id AND o.order_id = '$val'";
			$query = do_query($updateStorageSql) or die ("error:".mysqli_error());
		}
	}
	$result['error'] = "0";
	$result['content'] = "取消订单成功";
	die($json->encode($result));
	exit;
}
else{


	$order_info = select_one_line_all_items($table_Prefix.$table_suffix ,"order_id = ".$new_detail_nid);

	if($order_info == ''){
		$result['error'] = "1";
		$result['content'] = "当前订状态有误";

		die($json->encode($result));
		exit;
	}

	if($order_info['order_status'] == 2){

		$result['error'] = "1";
		$result['content'] = "当前订单已经是取消状态";

		die($json->encode($result));
		exit;
	}

	$res = do_query(" update $table set order_status = '2' WHERE order_id='$new_detail_nid'");

	//取消订单增加对应的库存
	$updateStorageSql =  "UPDATE ".$table_Prefix."dishes d,".$table_Prefix."order_detail o SET d.storage = d.storage + o.goods_number WHERE d.product_id = o.goods_id AND o.order_id = '$new_detail_nid'";
	$query = do_query($updateStorageSql) or die ("error:".mysqli_error());

	$result['error'] = "0";
	$result['content'] = "取消订单成功";

	die($json->encode($result));
	exit;
}
?>


